package map;

import java.util.*;

/**
 * Map的遍历
 */
public class MapDemo2 {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("语文", 100);
        map.put("数学", 99);
        map.put("英语", 90);
        map.put("化学", 80);
        map.put("物理", 99);
        System.out.println(map);

        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println("key:" + key);
        }

        /**
         * 遍历每一组键值对
         * Set<Entry> entrySet()
         * 将当前Map中所有的键值对以若干的Entry实例表示,并将他们以一个Set集合返回
         */

        Set<Map.Entry<String, Integer>> entry = map.entrySet();
        for (Map.Entry<String, Integer> e : entry) {
            String key = e.getKey();
            Integer value = e.getValue();
            System.out.println(key + ":" + value);
        }

        Collection<Integer> values = map.values();
        for (Integer v : values) {
            System.out.println("value:" + v);
        }

        values.forEach((value) -> {
            System.out.println(value);
        });

        values.forEach(System.out::println);

        map.forEach((k, v) -> System.out.println(k + ":" + v));
    }
}
